/**
 * Created by zhourb on 2018/2/26.
 */
var $ = require('jquery');
require('easyui');
require('layout/uiJs');
var $alert = require("libs/alert");
var $confirm = require("libs/confirm");
$(function () {
    $('#ft').show();
    $('#search').on('click',function () {
        var formData = $('#searchForm').serializeObject();
        formData.startDt = $('#startDt').data('utc0');
        formData.endDt = $('#endDt').data('utc0');
        formData.scene = 'Ledger';
        $('#tradeTable').datagrid('load',formData);
    });
    $(document).on('click','a[name=entDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'客户公司',true,true);
    });
    $(document).on('click','a[name=download]',function () {
        var id = $(this).data('id');
        var accountId = $(this).data('ent');
        window.location = "api/v1/p/mms/trade/exportBill?billingId="+id+'&accountId='+accountId;
    });
    tableInit();
    function tableInit() {
        $('#tradeTable').datagrid({
            url:'api/v1/p/mms/trade/accountTradeList',
            border : false,
            height:$(window).height(),
            pagination : true,
            pageSize : 20,
            pageList : [ 20, 50,  100 ],
            emptyMsg : "暂无数据",
            singleSelect: true,
            rownumbers:true,
            queryParams: {
                scene: 'Ledger'
            },
            columns:[[
                {
                    field:'entName',
                    title:'客户名称',
                    width:'20%',
                    formatter:function (value,row,index) {
                        return "<a class='ellipsis' href='javascript:;' ref='company-detail?id="+row.accountId+"' name='entDetail' title='"+value+"'>"+value+"</a>";
                    }
                },{
                    field:'currency',
                    title:'账期区间',
                    width:'20%',
                    formatter:function (value,row,index) {
                        return new Date(row.startDt.replace(/-/g,"/")).format('yyyy/MM/dd') + '-' + new Date(row.endDt.replace(/-/g,"/")).format('yyyy/MM/dd');
                    }
                },
                {
                    field:'beginningBalance',
                    title:'上期余额',
                    width:'12%'
                },{
                    field:'currentCharge',
                    title:'本期充值',
                    width:'12%'
                },{
                    field:'currentConsume',
                    title:'本期费用',
                    width:'12%'
                },{
                    field:'afterBalance',
                    title:'本期余额',
                    width:'12%'
                },{
                    field:'operation',
                    title:'下载账单',
                    width:'12%',
                    formatter:function (value,row,index) {
                        return "<a class='btn btn-default' href='javascript:;' name='download' data-id='"+row.id+"' data-ent='"+row.accountId+"'><i class='glyphicon glyphicon-download-alt'></i>下载</a>";
                    }
                }
            ]],
            loadFilter:function(data){
                if(data.code == '200'){
                    return data.data;
                }else{
                    return [];
                }
            },
            toolbar:"#tb"
        });
    }
});
